<template>
  <div>
    <div style="display: flex; justify-content: flex-start">
      <el-input
        placeholder="通过用户名搜索..."
        prefix-icon="el-icon-search"
        v-model="keywords"
        style="width: 400px"
        size="medium "
        clearable
      >
      </el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="mini"
        style="margin-left: 3px"
        @click="searchClick(keywords)"
        >搜索
      </el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column label="用户名" width="180">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">{{
            scope.row.username
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" width="180">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">{{
            scope.row.nickname
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">{{
            scope.row.creattime
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户角色" width="180">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">{{
            scope.row.role
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage1"
      :page-sizes="[5, 10, 15]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="修改用户信息" :visible.sync="dialogVisible" width="30%">
      <el-form
        label-width="100px"
        :model="user"
        class="form"
        ref="user"
      >
      <el-form-item
          label="昵称"
          prop="nickname"
          placeholder="请输入修改的用户昵称"
          width="200"
        >
          <el-input v-model="user.nickname"></el-input>
        </el-form-item>
        <el-form-item
          label="用户名"
          prop="username"
          placeholder="请输入修改的用户名"
          width="200"
        >
          <el-input v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="nickname"
          placeholder="请输入修改的用户密码"
          width="200"
        >
          <el-input v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item label="用户组" class="item">
          <el-select v-model="user.role" placeholder="请选择用户组">
            <el-option
              v-for="item in user.list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              ></el-option>
          </el-select>
        </el-form-item>
         
        <el-form-item>
          <el-button type="primary" size="small" @click="clickEdit"
            >修改用户</el-button
          >
          <el-button @click="go()">放弃修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { userlist, userdel ,edituser} from "@/api/apis";
export default {
  data() {
    return {
      currentPage1: 1,
      keywords: "",
      tableData: [],
      loading: false,
      pageSize: 5,
      total: 1,
      roles: "",
      dialogVisible: false,
      user: {
        username: "",
        nickname: "",
        role: "",
        password: "",
        list: [
          { label: "超级管理员", value: 2 },
          { label: "中级管理员", value: 1 },
          { label: "初级管理员", value: 0 },
        ],
        // uid: "",
      },
    };
  },
  created() {
    this.loading = true;
    this.roles = sessionStorage.getItem("role");

    this.refresh();
  },
  methods: {
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.refresh(this.pageSize);
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.refresh(this.currentPage1);
    },
    refresh() {
      userlist(this.pageSize, this.currentPage1, this.keywords, this.roles).then(
        (res) => {
          // console.log(res);
          this.tableData = res.data.data.records;
          this.total = res.data.data.total;
          // this.role=
          this.tableData.forEach((element) => {
            // console.log(element.role);
            if (element.role == 0) {
              element.role = "初级管理员";
            }
            if (element.role == 1) {
              element.role = "中级管理员";
            }
            if (element.role == 2) {
              element.role = "超级管理员";
            }
          });
          //   console.log(this.total);

          this.loading = false;
        }
      );
    },
    searchClick(keywords) {
      this.keywords = keywords;
      //   console.log(this.keywords)
      this.refresh(this.keywords);
    },
    handleDelete(index, row) {
      // console.log(row.uid);
      //   console.log(index);
      this.$confirm("此操作将永久删除该用户数据, 是否继续?", "温馨提示", {
        confirmButtonText: "狠心删除",
        cancelButtonText: "取消操作",
        type: "warning",
      })
        .then(() => {
          userdel(row.uid).then((res) => {
            // console.log(res);
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.refresh();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(row, index) {
      // console.log(row);
      // console.log( index);
      // this.$router.push({ path: "/main/users", query: { index: index } });
      // console.log(row.id);
      this.dialogVisible = true;
      this.user={...index, list: [
          { label: "超级管理员", value: 2 },
          { label: "中级管理员", value: 1 },
          { label: "初级管理员", value: 0 },
        ]}
      // console.log(this.user)   
    },
    clickEdit() {
      this.user.uid = this.user.uid;
      this.user.username = this.user.username;
      this.user.nickname = this.user.nickname;
      this.user.password = this.user.password;
      if (this.user.role == "初级管理员") {
        this.user.role = 0;
      }
      if (this.user.role == "中级管理员") {
        this.user.role = 1;
      }
      if (this.user.role == "超级管理员") {
        this.user.role = 2;
      }
      this.user.role = this.user.role;
      // console.log(this.user.role);
      // console.log(this.user);
      edituser(this.user).then((res) => {
        // console.log(res);
        if (res.data.code == 200) {
          this.$message({
            message: "恭喜你，账号修改成功",
            type: "success",
          });
          this.dialogVisible=false
          this.$router.push("/main/userlist");
          this.refresh()
        } else {
          this.$message.error("错了哦，账号修改失败");
        }
      });
    },
    go(){
      this.dialogVisible=false
    }
  },
};
</script>

<style scoped>
</style>